package com.softdev.system.generator.util;

import com.softdev.system.generator.entity.ClassInfo;
import com.softdev.system.generator.entity.FieldInfo;

import java.util.List;

public class CodeMaker {
    static String  makeJdbcTemplateCode(ClassInfo classInfo){
        String template="package zucc.kinect.jdbc;\n" +
                "\n" +
                "import lombok.extern.slf4j.Slf4j;\n" +
                "import org.springframework.beans.factory.annotation.Autowired;\n" +
                "import org.springframework.jdbc.core.JdbcTemplate;\n" +
                "import org.springframework.jdbc.support.rowset.SqlRowSet;\n" +
                "import org.springframework.stereotype.Repository;\n" +
                "import zucc.kinect.page.EquipRedPageTime;\n" +
                "import zucc.kinect.page.EquipRedPageTimeYuan;\n" +
                "import zucc.kinect.util.DecimalUtil;\n" +
                "import zucc.kinect.util.StrUtil;\n" +
                "\n" +
                "import java.sql.Timestamp;\n" +
                "import java.util.ArrayList;\n" +
                "import java.util.List;\n" +
                "\n" +
                "@Slf4j\n" +
                "@Repository\n" +
                "public class #className#JdbcT {\n" +
                "\n" +
                "    private JdbcTemplate jdbcTemplate;\n" +
                "\n" +
                "    @Autowired\n" +
                "    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {\n" +
                "        this.jdbcTemplate = jdbcTemplate;\n" +
                "    }\n" +
                "\n" +
                "\n" +
                "    String l(Object object) {\n" +
                "        return StrUtil.like(object);\n" +
                "    }\n" +
                "\n" +
                "    public List<EquipRedPageTimeYuan> list(EquipRedPageTimeYuan equipRedPageTimeYuan, Integer comId) {\n" +
                "\n" +
                "\n" +
                "        String equipRedPageSql = \"select ride.*,\\n\" +\n" +
                "                \"       tbl_user.user_name,\\n\" +\n" +
                "                \"       tbl_user.user_phone,\\n\" +\n" +
                "                \"       tbl_equipment.equipment_sid\\n\" +\n" +
                "                \"from ride,\\n\" +\n" +
                "                \"     tbl_user,\\n\" +\n" +
                "                \"     tbl_equipment\\n\" +\n" +
                "                \"where ride.user_id = tbl_user.id\\n\" +\n" +
                "                \"  and tbl_equipment.id = ride.equip_id \\n\";\n" +
                "        if (comId != null) {\n" +
                "            equipRedPageSql += \"  and company_id = ?\";\n" +
                "\n" +
                "        }\n" +
                "\n" +
                "\n" +
                "        Long id = equipRedPageTimeYuan.getId();\n" +
                "        Integer equipId = equipRedPageTimeYuan.getEquipId();\n" +
                "        Integer dayMaxRed = equipRedPageTimeYuan.getDayMaxRed();\n" +
                "        Integer todayCanRed = equipRedPageTimeYuan.getTodayCanRed();\n" +
                "        Timestamp changeDate = equipRedPageTimeYuan.getChangeDate();\n" +
                "        Integer deleteFlag = equipRedPageTimeYuan.getDeleteFlag();\n" +
                "//        传入null 的话 就有问题啊\n" +
                "//        if()\n" +
                "        String redStandStr = equipRedPageTimeYuan.getRedStand();\n" +
                "\n" +
                "        Integer redStand   = DecimalUtil.yuanToFen(equipRedPageTimeYuan.getRedStand());\n" +
                "\n" +
                "        Integer canRed = equipRedPageTimeYuan.getCanRed();\n" +
                "\n" +
                "        String ip = equipRedPageTimeYuan.getIp();\n" +
                "        String equipName = equipRedPageTimeYuan.getEquipName();\n" +
                "\n" +
                "\n" +
                "\n" +
                "        Integer redUp =  DecimalUtil.yuanToFen(equipRedPageTimeYuan.getRedUp());\n" +
                "        Integer redDown =  DecimalUtil.yuanToFen(equipRedPageTimeYuan.getRedDown());\n" +
                "        Integer dayCanRedVal =  DecimalUtil.yuanToFen(equipRedPageTimeYuan.getDayCanRedVal());\n" +
                "        Integer haveSendNum = equipRedPageTimeYuan.getHaveSendNum();\n" +
                "        String companyName = equipRedPageTimeYuan.getCompanyName();\n" +
                "\n" +
                "        if (id != null) {\n" +
                "//            sql += \" and id like \" + l(id);\n" +
                "            equipRedPageSql += \" and equipment_red_packet.id like \" + l(id);\n" +
                "        }\n" +
                "        if (equipId != null) {\n" +
                "//            sql += \" and id like \" + l(id);\n" +
                "            equipRedPageSql += \" and equipment_id like \" + l(equipId);\n" +
                "        }\n" +
                "\n" +
                "        if (dayMaxRed != null) {\n" +
                "            equipRedPageSql += \" and day_max_red like \" + l(dayMaxRed);\n" +
                "        }\n" +
                "\n" +
                "        if (todayCanRed != null) {\n" +
                "            equipRedPageSql += \" and today_can_red like \" + l(todayCanRed);\n" +
                "        }\n" +
                "\n" +
                "        if (changeDate != null) {\n" +
                "            equipRedPageSql += \" and equipment_red_packet.change_date like \" + l(changeDate);\n" +
                "        }\n" +
                "\n" +
                "        if (deleteFlag != null) {\n" +
                "            equipRedPageSql += \" and equipment_red_packet.delete_flag like \" + l(deleteFlag);\n" +
                "        }\n" +
                "\n" +
                "        if (redStand != null) {\n" +
                "            equipRedPageSql += \" and red_stand like \" + l(redStand);\n" +
                "        }\n" +
                "\n" +
                "        if (canRed != null) {\n" +
                "            equipRedPageSql += \" and can_red like \" + l(canRed);\n" +
                "        }\n" +
                "\n" +
                "        if (redUp != null) {\n" +
                "//            sql += \" and id like \" + l(id);\n" +
                "            equipRedPageSql += \" and red_up like \" + l(redUp);\n" +
                "        }\n" +
                "        if (redDown != null) {\n" +
                "//            sql += \" and id like \" + l(id);\n" +
                "            equipRedPageSql += \" and red_down like \" + l(redDown);\n" +
                "        }\n" +
                "\n" +
                "\n" +
                "        if (dayCanRedVal != null) {\n" +
                "            equipRedPageSql += \" and day_can_red_val like \" + l(dayCanRedVal);\n" +
                "        }\n" +
                "\n" +
                "        if (haveSendNum != null) {\n" +
                "            equipRedPageSql += \" and have_send_num like \" + l(haveSendNum);\n" +
                "        }\n" +
                "        if (companyName != null) {\n" +
                "            equipRedPageSql += \" and company_name like \" + l(companyName);\n" +
                "        }\n" +
                "\n" +
                "\n" +
                "\n" +
                "        if (ip != null) {\n" +
                "//            sql += \" and id like \" + l(id);\n" +
                "            equipRedPageSql += \" and ip like \" + l(ip);\n" +
                "        }\n" +
                "        if (equipName != null) {\n" +
                "//            sql += \" and id like \" + l(id);\n" +
                "            equipRedPageSql += \" and equipment_sid like \" + l(equipName);\n" +
                "        }\n" +
                "\n" +
                "        int pageIndex = equipRedPageTimeYuan.getPageIndex();\n" +
                "        int pageSize = equipRedPageTimeYuan.getPageSize();\n" +
                "\n" +
                "        String startTime = equipRedPageTimeYuan.getStartTime();\n" +
                "        String endTime = equipRedPageTimeYuan.getEndTime();\n" +
                "        if (startTime != null && endTime != null) {\n" +
                "            equipRedPageSql += \" and date(change_date)>= \" + StrUtil.quotation(startTime);\n" +
                "            equipRedPageSql += \" and date(change_date)<= \" + StrUtil.quotation(endTime);\n" +
                "        }\n" +
                "\n" +
                "        List<EquipRedPageTimeYuan> list = new ArrayList<>();\n" +
                "\n" +
                "        SqlRowSet sqlRowSet;\n" +
                "        if(comId==null){\n" +
                "            sqlRowSet = jdbcTemplate.queryForRowSet(equipRedPageSql);\n" +
                "\n" +
                "        }else{\n" +
                "            sqlRowSet = jdbcTemplate.queryForRowSet(equipRedPageSql, comId);\n" +
                "        }\n" +
                "        while (sqlRowSet.next()) {\n" +
                "            Long id1 = sqlRowSet.getLong(\"id\");\n" +
                "            Integer equipment_id = sqlRowSet.getInt(\"equipment_id\");\n" +
                "            Integer day_max_red = sqlRowSet.getInt(\"day_max_red\");\n" +
                "            Integer today_can_red = sqlRowSet.getInt(\"today_can_red\");\n" +
                "            Timestamp change_date = sqlRowSet.getTimestamp(\"change_date\");\n" +
                "            Integer delete_flag = sqlRowSet.getInt(\"delete_flag\");\n" +
                "            Integer red_stand = sqlRowSet.getInt(\"red_stand\");\n" +
                "            Integer can_red = sqlRowSet.getInt(\"can_red\");\n" +
                "            String ip1 = sqlRowSet.getString(\"ip\");\n" +
                "            String equipment_sid = sqlRowSet.getString(\"equipment_sid\");\n" +
                "\n" +
                "\n" +
                "            int red_up = sqlRowSet.getInt(\"red_up\");\n" +
                "            int red_down = sqlRowSet.getInt(\"red_down\");\n" +
                "            int day_can_red_val = sqlRowSet.getInt(\"day_can_red_val\");\n" +
                "            int have_send_num = sqlRowSet.getInt(\"have_send_num\");\n" +
                "            String company_name = sqlRowSet.getString(\"company_name\");\n" +
                "\n" +
                "\n" +
                "            EquipRedPageTime equipRedPageTime1 = new EquipRedPageTime(id1, equipment_id, change_date, ip1, red_stand,\n" +
                "                    equipment_sid, day_max_red, today_can_red, can_red, delete_flag, pageIndex, pageSize,\n" +
                "                    startTime, endTime,red_up,red_down,day_can_red_val,have_send_num,company_name);\n" +
                "            EquipRedPageTimeYuan equipRedPageTimeYuanRes = equipRedPageTime1.toYuan();\n" +
                "\n" +
                "            list.add(equipRedPageTimeYuanRes);\n" +
                "        }\n" +
                "        return list;\n" +
                "\n" +
                "\n" +
                "    }\n" +
                "}\n";
        List<FieldInfo> fieldList = classInfo.getFieldList();
        for (FieldInfo fieldInfo : fieldList) {
            String fieldName = fieldInfo.getFieldName();
            String columnName = fieldInfo.getColumnName();
//            这个应该是下划线的吧
        }
        template=  template.replace("#className#",classInfo.getClassName());
        return null;
    }
}
